package hluck.me.srorage.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hluck.me.srorage.sys.common.Constast;
import hluck.me.srorage.sys.common.DataGridView;
import hluck.me.srorage.sys.common.ResultObj;
import hluck.me.srorage.sys.common.TreeNode;
import hluck.me.srorage.sys.domain.SysPermission;
import hluck.me.srorage.sys.domain.SysRole;
import hluck.me.srorage.sys.service.SysPermissionService;
import hluck.me.srorage.sys.service.SysRoleService;
import hluck.me.srorage.sys.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysPermissionService permissionService;
    /**
     * 查询
     */
    @RequestMapping("loadAllRole")
    public DataGridView loadAllRole(RoleVo roleVo){
        Page<SysRole> page = new Page<>(roleVo.getPage(), roleVo.getLimit());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(roleVo.getName()),"name",roleVo.getName());
        queryWrapper.like(StringUtils.isNotBlank(roleVo.getRemark()),"remark",roleVo.getRemark());
        queryWrapper.eq(roleVo.getAvailable()!=null,"available",roleVo.getAvailable());
        queryWrapper.orderByDesc("createtime");

        this.sysRoleService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加角色
     */
    @RequestMapping("addRole")
    public ResultObj addRole(RoleVo noticeVo){
        try {
            noticeVo.setCreatetime(new Date());
            sysRoleService.save(noticeVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 更新角色
     */
    @RequestMapping("updateRole")
    public ResultObj updateRole(RoleVo noticeVo){
        try {
            sysRoleService.updateById(noticeVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除角色
     */
    @RequestMapping("deleteRole")
    public ResultObj deleteRole(Integer id){
        try{
            this.sysRoleService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 根据角色id加载菜单和权限树的json串
     */
    @RequestMapping("initPermissionByRoleId")
    public DataGridView ininPermissionByRoleId(Integer roleId){
        //查询所有可用的菜单和权限
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available", Constast.AVAILABLE_TRUE);
        List<SysPermission> allPermissions = permissionService.list(queryWrapper);
        /**
         * 1.根据角色id查询当前角色拥有的所有的权限或菜单ID
         * 2.根据查询出来的菜单ID查询权限和菜单数据
         */
        List<Integer> currentRolePermissions=this.sysRoleService.getAllPid(roleId);
        List<SysPermission> currentPermissions=null;
        if (currentRolePermissions.size()>0){
            queryWrapper.in("id",currentRolePermissions);
            currentPermissions = permissionService.list(queryWrapper);
        }else {
            currentPermissions=new ArrayList<>();
        }

        //构造List<TreeNode>
        ArrayList<TreeNode> treeNodes = new ArrayList<>();
        for (SysPermission p1 : allPermissions) {
            String checkArr="0";
            for (SysPermission p2 : currentPermissions) {
                if (p1.getId()==p2.getId()) {
                    checkArr="1";
                }
            }
            Boolean spread=p1.getOpen()==null||p1.getOpen()==1?true:false;
            treeNodes.add(new TreeNode(p1.getId(),p1.getPid(),p1.getTitle(),spread,checkArr));
        }
        return new DataGridView(treeNodes);
    }

    /**
     * 保存角色和菜单之间的关系
     */
    @RequestMapping("saveRolePermission")
    public ResultObj saveRolePermission(Integer rid,Integer[] ids){
        try{

            this.sysRoleService.saveRolePermission(rid,ids);
            return ResultObj.DISPATCH_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DISPATCH_ERROR;
        }
    }

}

